﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Viewer
{
    public abstract class Camera : GameComponent
    {
        protected KeyboardState kbd, lastKbd;
        protected MouseState mouse, lastMouse;

        public Vector3 MouseRate = new Vector3(0.005f, 0.005f, 0.05f); // x/y - pan, z - zoom
        public Vector3 KbdRate = new Vector3(0.5f, 0.5f, 5f);
        
        protected Vector3 mouseDelta = Vector3.Zero;

        public float MouseWheelRate 
        { 
            get { return MouseRate.Z / 10f; }
            set { MouseRate.Z = value * 10f; }
        }

        protected float MouseWheelDelta { get { return mouseDelta.Z; } }

        protected abstract void HandleInput();

        public Camera(Game game)
            : base(game)
        {
        }

        public override void Initialize()
        {
            base.Initialize();

            kbd = Keyboard.GetState();
            mouse = Mouse.GetState();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            lastKbd = kbd;
            lastMouse = mouse;

            if (Game.IsActive)
            {
                kbd = Keyboard.GetState();
                mouse = Mouse.GetState();

                mouseDelta.X = (mouse.X - lastMouse.X);
                mouseDelta.Y = (mouse.Y - lastMouse.Y);
                mouseDelta.Z = (mouse.ScrollWheelValue - lastMouse.ScrollWheelValue);

                HandleInput();
            }

        }

        public abstract void Home();
    }
}
